<?php
#基础函数
include_once('_path.php');

if (BENCHMARK) {
	require_once(_CONST_LOCAL_INCLUDE_PATH.'/Benchmark/Timer.php');
	$timer = new Benchmark_Timer();
	$timer->start();
}

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

if (BENCHMARK) $timer->setMarker('init');

#登录权限
check_loggedin();

#用户信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

if (BENCHMARK) $timer->setMarker('auth');

$token = md5(uniqid().time());
$_SESSION['token'] = $token;

#cache lite
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite.php');
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite/Output.php');

$cache = new Cache_Lite_Output();

#用于meta的keywords标签
$keywords = "";
#用于meta的Description标签
$description = "";
#用于mete的http-equiv标签
$referrer = "";
#用于meta的title标签
$title = "上传商品照片 - Votings";
#用于获取针对本页的css样式
$css = array();
#用于获取针对本页的js
$js = array("jquery.masonry.min");
#用于获取针对head的其他标签
$links = array();
#用于控制那个主菜单为
$o = "";

$ads = array();

$post_code =<<<HTML
HTML;

if (BENCHMARK) $timer->setMarker('proc');

//if ($user['loggedin'] < 1)
//	cache_header(900);

no_cache_header();

#header
include_once(_CONST_LOCAL_LAYOUT_PATH."_header.php");
?>
<script>
$(function(){
	//HIDE next-setp BUTTON
    $("#next-step").hide();
	//AJAX GET REMOTE IMAGE
	$(".get-pic").click(function(){
		var gurl = $("#url").val();
		if	(!vote.isURL(gurl)) {
			alert("请输入合法的URL地址");
			return;
		}
		
		if($("#gurl").val()==gurl)
		{
			return;
		}
		
		$(this).attr('disabled',"true");
		
		$('.pin-loading').show();	
		$.getJSON("ajax/getimage.php", { url: gurl }, function(json) {
			if(json.s == 1){
			$(".box-header").show();
		    $("#container").show();
			    //GET TITLE
				$("#title").val(json.title);
				//GET IMAGE
				$.each(json.url, function(i, field){
				  $("#container").append("<div class=\"pin\"><a href=\"###\"><img src=\""+field+"\" /></a></div>");
				});
				//IMAGE STYLE
				var $container = $('#container');
				$container.imagesLoaded( function() {
					$container.masonry({
						itemSelector : '.pin',
						isAnimated: 'true'
					});
				}); 
				//$('#container').masonry( 'reload' );
				//SET GURL
				$("#gurl").val(gurl);
				
				//SELECT GIMG
				$(".pin").click(function(){
					$(".pin").removeClass("selected");
					$(this).addClass("selected");
					$("#gimg").val($(this).children().children("img").attr("src"));
					 $("#next-step").show();
				});	
				
				$('.pin-loading').hide();
			}else{
				alert("获取图片出错!");
			}
			$(this).removeAttr("disabled");
		});
	});	
	
<?php
if(isset($_SESSION['step_1'])){
?>
    $('.pin-loading').show();
	$.getJSON("ajax/getimage.php", { url: "<?php echo $_SESSION['step_1']['gurl']?>" }, function(json) {
		if(json.s == 1){
		    $(".box-header").show();
		    $("#container").show();
			//GET IMAGE
			$.each(json.url, function(i, field){
				if(field == "<?php echo $_SESSION['step_1']['gimg']?>"){
					$("#container").append("<div class=\"pin selected\"><a href=\"###\"><img src=\""+field+"\" /></a></div>");
				}else{
					$("#container").append("<div class=\"pin\"><a href=\"###\"><img src=\""+field+"\" /></a></div>");
				}
			});
			//IMAGE STYLE
			var $container = $('#container');
			$container.imagesLoaded( function() {
				$container.masonry({
					itemSelector : '.pin',
					isAnimated: 'true'
				});
			});
			//$('#container').masonry( 'reload' );
			
			//SET GURL
			$("#gurl").val('<?php echo $_SESSION['step_1']['gurl']?>');						
			//GET URL
			$("#url").val('<?php echo $_SESSION['step_1']['gurl']?>');
			
			//SHOW next-setp BUTTON
			$("#next-step").show();
			
			//SELECT GIMG
			$(".pin").click(function(){
				$(".pin").removeClass("selected");
				$(this).addClass("selected");
				$("#gimg").val($(this).children().children("img").attr("src"));
				$("#next-step").show();
			});	
			
			//GET TITLE
			$("#title").val('<?php echo $_SESSION['step_1']['title']?>');
			//GET IMGURL
			$("#gimg").val('<?php echo $_SESSION['step_1']['gimg']?>');

            $('.pin-loading').hide();			
		}else{
			alert("获取图片出错!");
		}
	});
<?php
}
?>
});
</script>
<div id="main">
<form name="step-1" id="step-1" action="flow-2.php" method="post">
<input name="gurl" id="gurl" type="hidden" value="">
<input name="gimg" id="gimg" type="hidden" value="">
<input name="step" id="step" type="hidden" value="do">
<input name="token" id="token" type="hidden" value="<?php echo $token;?>">
	<div class="box">
		<ul class="flow clearfix f1">
			<li class="cur">1、选择商品图片</li>
			<li>2、选择自己的照片</li>
			<li>3、添加一段描述</li>
			<li>4、发布投票</li>
		</ul>
		<div class="box-header"><h1>选择衣服图片</h1></div>
		<div class="input-wrap clearfix">
			<label for="">商品购买地址</label>
			<input type="text" name="url" id="url" class="product-url" value="Paste a link to web-page containing images of cloths you want to buy" onfocus = "this.value=''" onblur = "if(this.value==''){this.value='Paste a link to web-page containing images of cloths you want to buy'}"/>
			<input type="button" name="get-pic" class="get-pic" value="" />
			<div class="pin-loading" style="display:none;"><span>正在获取图片</span></div>
		</div>
		<div class="input-wrap clearfix">
			<label for="">投票标题</label>
			<input type="text" name="title" id="title" class="product-url" value="This text will be shown as the voting title" onfocus = "this.value=''" onblur = "if(this.value==''){this.value='This text will be shown as the voting title'}" />
		</div>
		
		<div class="box-header" style="display:none"><h1>选择以下图片将出现在投票中</h1></div>
		<div id="container" class="photo-wrap" style="display:none">
		</div>
		<div class="next-step"><input type="button" id="next-step" name="next-step" class="next" value="" onclick="vote.checkForm();" /></div>
	</div>
</form>	
</div>
<!--main end-->
<?php
//footer
include_once (_CONST_LOCAL_LAYOUT_PATH."_footer.php");

if (BENCHMARK) $timer->setMarker('page');

if (BENCHMARK) {
	echo "<!-- init: ".$timer->timeElapsed('Start', 'init')." -->\n";
	echo "<!-- auth: ".$timer->timeElapsed('init', 'auth')." -->\n";
	echo "<!-- proc: ".$timer->timeElapsed('auth', 'proc')." -->\n";
	echo "<!-- page: ".$timer->timeElapsed('proc', 'page')." -->\n";
	echo "<!-- total: ".$timer->timeElapsed('Start', 'Stop')." -->\n";
}

?>
